package hongcaosp.app.android.user.settings.invite;

import java.util.List;

import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.contact.ListPresenter;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.InviteRecord;
import hongcaosp.app.android.modle.bean.Pagebean;
import hongcaosp.app.android.modle.impl.PrizeModelImpl;

/**
 * 邀请有礼记录
 */
public class InviteRecordPresenter extends ListPresenter {
    private IListView<InviteRecord> listView;
    private PrizeModelImpl prizeModel;

    private int number;
    private int total;

    public InviteRecordPresenter(IListView<InviteRecord> listView) {
        this.listView = listView;
        prizeModel = new PrizeModelImpl();
    }

    public void fresh() {
        number = 1;
        prizeModel.invitationRecord(UserToken.getDefault().getToken(),number, 20,
                new DataCallBack<Pagebean<InviteRecord>>() {
                    @Override
                    public void onGetData(Pagebean<InviteRecord> data) {
                        super.onGetData(data);
                        boolean loadMore = false;
                        total = data.getList() == null ? 0 : data.getList().size();
                        if (data.getTotal() > total) {
                            loadMore = true;
                        }
                        List<InviteRecord> recordList = data.getList();
//                List<Message> videoList = new ArrayList<>();
//                videoList.addAll(videos);
                        listView.freshList(recordList, loadMore);
                    }

                    @Override
                    public void onFail(int code, String errorMessage) {
                        super.onFail(code, errorMessage);
                        listView.freshList(null, false);
                    }
                });
    }

    public void loadMoreData() {
        number++;
        prizeModel.invitationRecord(UserToken.getDefault().getToken(),  number, 20,
                new DataCallBack<Pagebean<InviteRecord>>() {
                    @Override
                    public void onGetData(Pagebean<InviteRecord> data) {
                        super.onGetData(data);
                        boolean loadMore = false;
                        total += data.getList() == null ? 0 : data.getList().size();
                        if (data.getTotal() > total) {
                            loadMore = true;
                        }
                        List<InviteRecord> recordList = data.getList();
//                List<Message> videoList = new ArrayList<>();
//                videoList.addAll(videos);
                        listView.addList(recordList, loadMore);
                    }

                    @Override
                    public void onFail(int code, String errorMessage) {
                        super.onFail(code, errorMessage);
                        listView.addList(null, false);
                    }
                });
    }

    @Override
    public void freshData() {
        fresh();
    }

    @Override
    public void loadMore() {
        loadMoreData();
    }
}
